//
//  HD_NKM_MapView+Line.m
//  HDWuDaDaoServiceCenter
//
//  Created by HDNiuKuiming on 2017/5/25.
//  Copyright © 2017年 HDNiuKuiming. All rights reserved.
//

#import "HD_NKM_MapView+Line.h"

@implementation HD_NKM_MapView (Line)
-(void)strokeChart:(NSArray *)pointArray
{
    for (int i=0; i<pointArray.count; i++) {
        NSValue *pntValue = pointArray[i];
        CGPoint linePoint = pntValue.CGPointValue;
        
        //划线
        CAShapeLayer *_chartLine = [CAShapeLayer layer];
        _chartLine.lineCap = kCALineCapRound;
        _chartLine.lineJoin = kCALineJoinBevel;
        _chartLine.fillColor   = [[UIColor whiteColor] CGColor];
        _chartLine.lineWidth   = 2.0;
        _chartLine.strokeEnd   = 0.0;
        [self.layer addSublayer:_chartLine];
        
        UIBezierPath *progressline = [UIBezierPath bezierPath];
        [self addPoint:linePoint
                 index:i
                isShow:YES
                 value:0];
        
        [progressline moveToPoint:linePoint];
        [progressline setLineWidth:2.0];
        [progressline setLineCapStyle:kCGLineCapRound];
        [progressline setLineJoinStyle:kCGLineJoinRound];
        
        NSInteger j=i+1;
        if (pointArray.count > j)
        {
            NSValue *pntValue = pointArray[j];
            CGPoint point = pntValue.CGPointValue;
            [progressline addLineToPoint:point];
            
            [progressline moveToPoint:point];
            [self addPoint:point
                     index:j
                    isShow:YES
                     value:0];
        }
        
        _chartLine.path = progressline.CGPath;
        _chartLine.strokeColor = [UIColor greenColor].CGColor;
        
        CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
        pathAnimation.duration = pointArray.count*0.4;
        pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
        pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];
        pathAnimation.autoreverses = NO;
        [_chartLine addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
        
        _chartLine.strokeEnd = 1.0;
    }
}

// 画圈方法
- (void)addPoint:(CGPoint)point index:(NSInteger)index isShow:(BOOL)isHollow value:(CGFloat)value
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(5, 5, 8, 8)];
    view.center = point;
    view.layer.masksToBounds = YES;
    view.layer.cornerRadius  = 4;
    view.layer.borderWidth   = 2;
    view.layer.borderColor   = [UIColor greenColor].CGColor;
    
    if (isHollow) {
    }else{
        view.backgroundColor = [UIColor whiteColor];
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(point.x-2, point.y-2, 4, 4)];
        label.font = [UIFont systemFontOfSize:10];
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = view.backgroundColor;
        label.text = [NSString stringWithFormat:@"%d",(int)value];
        [self addSubview:label];
    }
    
    [self addSubview:view];
}


@end
